//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelComposite.h"
using namespace Cephei::QL::Models::Marketmodels::Products;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProduct.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (boost::shared_ptr<QuantLib::MarketModelComposite>& childNative, Object^ owner) : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
#ifdef HANDLE
	_phMarketModelComposite = NULL;
#endif
	_ppMarketModelComposite = &childNative;
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
}
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (QuantLib::MarketModelComposite& childNative, Object^ owner) : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
#ifdef HANDLE
	_phMarketModelComposite = NULL;
#endif
	_ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (&childNative);
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
    _MarketModelCompositeOwner = owner;
    _MarketModelMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (CMarketModelComposite^ copy) : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
#ifdef HANDLE
	_phMarketModelComposite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (copy->GetShared());
        _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
    }
}
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (PLATFORM::Type^ t) : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
#ifdef HANDLE
	_phMarketModelComposite = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelComposite::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (QuantLib::Handle<QuantLib::MarketModelComposite>& childNative, Object^ owner)  : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
	_phMarketModelComposite = &childNative;
	_ppMarketModelComposite = &static_cast<boost::shared_ptr<QuantLib::MarketModelComposite>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
    _MarketModelCompositeOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (QuantLib::Handle<QuantLib::MarketModelComposite> childNative)  : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
	_phMarketModelComposite = &childNative;
	_ppMarketModelComposite = &static_cast<boost::shared_ptr<QuantLib::MarketModelComposite>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::CMarketModelComposite (QuantLib::MarketModelComposite childNative)  : CMarketModelMultiProduct(CMarketModelComposite::typeid)
{
#ifdef HANDLE
	_phMarketModelComposite = NULL;
#endif
	_ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (new QuantLib::MarketModelComposite (childNative));
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelComposite));
}
#endif

Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::~CMarketModelComposite ()
{
    if (_ppMarketModelComposite != NULL)
    {
	    delete _ppMarketModelComposite;
        _ppMarketModelComposite = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::!CMarketModelComposite ()
{
    if (_ppMarketModelComposite != NULL)
    {
	    delete _ppMarketModelComposite;
    }
}
QuantLib::MarketModelComposite& Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::GetReference ()
{
    if (_ppMarketModelComposite == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelComposite;
}
boost::shared_ptr<QuantLib::MarketModelComposite>& Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::GetShared ()
{
    if (_ppMarketModelComposite == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelComposite;
}
QuantLib::MarketModelComposite* Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::GetPointer ()
{
    if (_ppMarketModelComposite == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelComposite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelComposite>& Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::GetHandle ()
{
	if (_phMarketModelComposite == NULL)
	{
		_phMarketModelComposite = new Handle<QuantLib::MarketModelComposite> (*_ppMarketModelComposite);
	}
	return *_phMarketModelComposite;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::HasNative () 
{
	return (_ppMarketModelComposite != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelComposite)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IMarketModelMultiProduct^ Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::Item (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::MarketModelMultiProduct& _rv = (QuantLib::MarketModelMultiProduct&)(*_ppMarketModelComposite)->item ( _i );   
        Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::Multiplier (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelComposite)->multiplier ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelComposite)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::IMarketModelComposite^ Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelComposite)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelComposite)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::CMarketModelComposite::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelComposite)->suggestedNumeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

